/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($http, httpservice, $q) {
    var caseBarDetailxiaService = {};


    /*获取当事人列表*/
    caseBarDetailxiaService.getDSRList = function (caseId) {
        var params = {"caseId": caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getCaseClientsUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*获取对方当事人列表*/
    caseBarDetailxiaService.getDFDSRList = function (caseId) {
        var params = {"caseId": caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getDFDSRUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*添加当事人*/
    caseBarDetailxiaService.addDSR = function (caseId, caseType, caseClientType, AdversaryClientName, userId,clientId) {
        var params = {
            "caseId": caseId,
            "caseType": caseType,
            "caseClientType": caseClientType,
            "caseClientName": AdversaryClientName,
            "userId": userId,
            "clientId":clientId
        };
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addDSRUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*添加对方当事人*/
    caseBarDetailxiaService.addDFDSR = function (caseId, caseType, caseClientType, AdversaryClientName, userId) {
        var params = {
            "caseId": caseId,
            "caseType": caseType,
            "caseClientType": caseClientType,
            "adversaryClientName": AdversaryClientName,
            "userId": userId
        };
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addDFDSRUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*查询自然人列表的接口*/
    caseBarDetailxiaService.selectZiRanRenInfo = function (biglawuserid, pageSize, page, name) {
        var deferred = $q.defer();
        var params = {'pageNum': page, 'pageSize': pageSize, 'biglawuserid': biglawuserid, 'name': name};
        $http({
            method: 'GET',
            url: httpservice.selectZRRCustomerUrl,
            params: params,
            dataType: 'json'
            //headers: {'Content-Type': 'application/json'}

        }).then(function successCallback(response) {
            //当前页
            var maxPage = page;
            if (maxPage === "") {
                maxPage = 1;
            }
            //总页数
            var total = response.data.totalPage;
            var startPage = response.data.beginPage;
            var endPage = response.data.endPage;
            var totalCount = response.data.totalCount;
            if (total <= maxPage) {
                maxPage = total;
            }
            if (maxPage <= 0) {
                maxPage = 1;
            }
            var totalPas = {};
            for (var i = startPage; i <= endPage; i++) {
                totalPas[i - 1] = i;
            }
            //设置当前页和总页数
            response.data.totalPage = total;
            // response.data.nowPage = maxPage + "/" + total;
            response.data.totalPas = totalPas;
            response.data.totalCount = totalCount;

            // response.data.page = maxPage;
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response.data);
        });
        return deferred.promise;
    };
    /*查询法人列表的接口*/
    caseBarDetailxiaService.selectFaRenInfo = function (loginId, pageSize, page, companyname) {
        var deferred = $q.defer();
        var params = {'pageNum': page, 'pageSize': pageSize, 'biglawuserid': loginId, 'companyname': companyname};
        $http({
            method: 'GET',
            url: httpservice.selectFRCustomerUrl,
            params: params,
            dataType: 'json'
            //headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            //当前页
            var maxPage = page;
            if (maxPage === "") {
                maxPage = 1;
            }
            //总页数
            var total = response.data.totalPage;
            var startPage = response.data.beginPage;
            var endPage = response.data.endPage;
            var totalCount = response.data.totalCount;
            if (total <= maxPage) {
                maxPage = total;
            }
            if (maxPage <= 0) {
                maxPage = 1;
            }
            var totalPas = {};
            for (var i = startPage; i <= endPage; i++) {
                totalPas[i - 1] = i;
            }
            //设置当前页和总页数
            response.data.totalPage = total;
            // response.data.nowPage = maxPage + "/" + total;
            response.data.totalPas = totalPas;
            response.data.totalCount = totalCount;
            response.data.page = maxPage;
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response.data);
        });
        return deferred.promise;
    };

    //添加自然人
    caseBarDetailxiaService.addZRRCustomer = function (loginId, name, sex, phone, birthday, nationality, profession, workplace, address, status, remark) {
        var params = {
            "biglawuserid": loginId,
            "name": name,
            "sex": sex,
            "contactnumber": phone,
            "birthday": birthday,
            "nationality": nationality,
            "profession": profession,
            "workplace": workplace,
            "address": address,
            "status": status,
            "remark": remark
        };
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addZRRCustomerUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //添加法人
    caseBarDetailxiaService.addFRCustomer = function (loginId, companyname, address, representative, post, contactname, contactphone,workplace, status, remark,customerGroupId) {//
        var params = {
            "biglawuserid": loginId,
            "companyname": companyname,
            "address": address,
            "representative": representative,
            "post": post,
            "contactname": contactname,
            "contactphone": contactphone,
            "workplace": workplace,
            "status": status,
            "remark": remark,
            "customerGroupId":customerGroupId,
        };
        return $http(
            {
                url: httpservice.addFRCustomerUrl,
                method: 'POST',
                params: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };


    /*获取省列表*/
    caseBarDetailxiaService.getProvinceList = function () {
        //var params={"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getCourtListUrl,
            //params:params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //律师成员
    caseBarDetailxiaService.CaseLawyerTian = function (keyword, teamId, caseId) {
        //此处修改了 搜索的参数
        var params = {"keyword": keyword,"teamId":teamId,"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.CaseZhiTianUrl,
            params: params,
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*获取法院列表*/
    caseBarDetailxiaService.getCourtList = function (params) {
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getCourtListUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*上传法院,案件概述,诉讼请求*/
    caseBarDetailxiaService.putCourtAJSS = function (params) {
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.editCaseDetailUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*案件详情*/
    caseBarDetailxiaService.getCaseDetails = function (caseId) {
        var params = {"caseId": caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getCaseDetailUrl,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    /*修改律师*/
    caseBarDetailxiaService.modificationbarrister = function (json) {
        // var params={"caseId":caseId,};
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.modificationUrl,
            data: json,
            // headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 删除律师
     * @param id            Int	主键ID
     * @param userId        String	用户id
     * @param lawyerType    String	律师身份：0 - 未知；1 - 主办律师；2 - 案源律师；3 - 协助律师；5-其它；
     * @param caseId        String	案件id
     * @param lawyeName     String	律师名称
     * @param lawyerId      律师id
     * @param loginName     登录用户名
     * @param caseName      案件名
     * @returns {Promise}
     */
    caseBarDetailxiaService.deletebarrister = function (params) {
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteCaseLawyer,
            params: params
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    /*关注列表*/
    caseBarDetailxiaService.Attention = function (caseId, page, pagesize) {
        var params = {"caseId": caseId, "page": page, "pagesize": pagesize};
        var deferred = $q.defer();
        $http({
            method: 'post',
            url: httpservice.ListByCaseId,
            params: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**取消收藏*/
    caseBarDetailxiaService.Enshrine = function (relatedId, caseId, relatedStatus, userId, relatedName) {
        //userId：用户id，relatedName：相关案例名称
        var params = {
            "relatedId": relatedId,
            "caseId": caseId,
            "relatedStatus": relatedStatus,
            "userId": userId,
            "relatedName": relatedName
        };
        // var  params ={"relatedId":"17001","caseId":"3","relatedStatus":"0"};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.CollectionRelatedCaseUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    /**
     * 修改当事人名字
     * @param userId 用户ID
     * @param caseId 案件ID
     * @param relatedStatus 状态：1-当事人，2-对方当事人，3-审判人员，4-第三人
     * @param relatedName 修改后的名字
     * @param relatedId 相关人员ID
     * @returns {Promise}
     */
    caseBarDetailxiaService.updateCaseRelatedPersonName = function (userId, caseId, relatedStatus, relatedName, relatedId,casename) {
        var params = {
            "userId": userId,
            "caseId": caseId,
            "relatedStatus": relatedStatus,
            "relatedName": relatedName,
            "relatedId": relatedId,
            "caseName": casename
        };
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateCaseRelatedPersonNameUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 删除当事人
     * @param id
     * @param caseId
     * @param caseClientName
     * @param userId
     * @param caseName
     * @returns {Promise}
     */
    caseBarDetailxiaService.deleteDSR = function (id, caseId, caseClientName, userId, caseName) {
        var params = {
            "id": id,
            "caseId": caseId,
            "caseClientName": caseClientName,
            "userId": userId,
            "caseName": caseName
        };
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteDSRUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    /**
     * 删除对方当事人
     * @param id
     * @param caseId
     * @param adversaryClientName
     * @param userId
     * @returns {Promise}
     */
    caseBarDetailxiaService.deleteDFDSR = function (id, caseId, adversaryClientName, userId) {
        var params = {"id": id, "caseId": caseId, "adversaryClientName": adversaryClientName, "userId": userId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteDFDSRUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //案由
  // $scope.caseReasonList = caseReasonList;
    caseBarDetailxiaService.caseReason = function (params) {
      return $http({
        method:"POST",
        url:httpservice.caseReasonUrl,
        params:params
      })
    };

    //业务类型下拉
    caseBarDetailxiaService.caseTrialLevel = function (params) {
      return $http({
        method:"GET",
        url:httpservice.CaseTrialLevelUrl,
        params:params
      })
    };


    return caseBarDetailxiaService;
};
